﻿using Joker.Model;
using Joker.View.Editor;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Management {
    public partial class GoodsManage : Form {
        private static string SqlStr = "SELECT * FROM [Goods]";
        private static bool Search = true;
        public GoodsManage() {
            InitializeComponent();
        }

        private void GoodsManage_Load(object sender, EventArgs e) {
            dataGridView1.DataSource = DBHelper.GetDataTable(SqlStr);
        }

        private void button1_Click(object sender, EventArgs e) {
            if (Search) {
                Search = false;
                button1.Text = "重置";
                dataGridView1.DataSource = DBHelper.GetDataTable($"{SqlStr} WHERE Name LIKE '%{textBox1.Text}%'");
            } else {
                Search = true;
                button1.Text = "查询";
                dataGridView1.DataSource = DBHelper.GetDataTable(SqlStr);
            }
        }
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e) {
            GoodsDataEdit edit = new GoodsDataEdit();
            OpenForm.NewForm(this, edit, false);
            dataGridView1.DataSource = DBHelper.GetDataTable(SqlStr);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) {
            if (dataGridView1.CurrentRow == null || dataGridView1.CurrentRow.Index >= dataGridView1.Rows.Count || dataGridView1.CurrentRow.Index < 0) {
                MessageBox.Show("请先选定要修改的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            } else {
                GoodsDataEdit edit = new GoodsDataEdit();
                edit.GoodsID = (int)dataGridView1.CurrentRow.Cells[0].Value;
                OpenForm.NewForm(this, edit, false);
            }
            dataGridView1.DataSource = DBHelper.GetDataTable(SqlStr);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {
            if (dataGridView1.CurrentRow == null || dataGridView1.CurrentRow.Index > dataGridView1.Rows.Count) {
                MessageBox.Show("请先选定要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            } else {
                DialogResult res = MessageBox.Show($"你确定要删除ID为{dataGridView1.CurrentRow.Cells[0].Value}的数据？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (res == DialogResult.OK) {
                    string sqlStr = $"DELETE FROM [Goods] WHERE ID = '{dataGridView1.CurrentRow.Cells[0].Value}'";
                    if (DBHelper.ExecuteNonQuery(sqlStr)) {
                        MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    } else {
                        MessageBox.Show("删除失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    dataGridView1.DataSource = DBHelper.GetDataTable(SqlStr);
                };
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
            if (e.RowIndex > 0 && e.RowIndex < dataGridView1.Rows.Count) dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];
        }
    }
}
